package com.digicom.admin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.digicom.common.constant.Constants;
import com.digicom.common.entity.Comment;
import com.digicom.common.core.domain.ApiResponse;
import com.digicom.common.core.domain.TableDataInfo;
import com.digicom.common.core.utils.SecurityUtils;
import com.digicom.service.comment.CommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 评论管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/admin/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    /**
     * 查询评论列表
     */
    @PreAuthorize("@ss.hasPermi('system:comment:list')")
    @GetMapping("/list")
    public TableDataInfo list(Comment comment) {
        // 构建分页对象
        Page<Comment> page = new Page<>(comment.getPageNum(), comment.getPageSize());

        // 查询待审核评论
        Page<Comment> commentPage;
        if (Constants.COMMENT_STATUS_PENDING.equals(comment.getStatus())) {
            commentPage = commentService.listPendingComments(page);
        } else {
            // 这里应该根据评论查询条件查询评论列表
            // 此处简化处理，实际应该根据文章标题、用户名等条件进行查询
            commentPage = commentService.listPendingComments(page);
        }

        return new TableDataInfo(commentPage.getRecords(), commentPage.getTotal());
    }

    /**
     * 获取评论详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:comment:query')")
    @GetMapping("/{commentId}")
    public ApiResponse<Comment> getInfo(@PathVariable Long commentId) {
        return ApiResponse.success(commentService.getById(commentId));
    }

    /**
     * 审核通过评论
     */
    @PreAuthorize("@ss.hasPermi('system:comment:approve')")
    @PutMapping("/approve")
    public ApiResponse<Boolean> approve(@RequestBody Comment comment) {
        if (comment.getCommentId() == null) {
            return ApiResponse.error("参数不能为空");
        }

        // 设置状态为已通过
        boolean result = commentService.reviewComment(
            comment.getCommentId(),
            Constants.COMMENT_STATUS_NORMAL,
            SecurityUtils.getUsername(),
            comment.getReviewRemark());

        return result ? ApiResponse.success() : ApiResponse.error("审核失败");
    }

    /**
     * 驳回评论
     */
    @PreAuthorize("@ss.hasPermi('system:comment:reject')")
    @PutMapping("/reject")
    public ApiResponse<Boolean> reject(@RequestBody Comment comment) {
        if (comment.getCommentId() == null) {
            return ApiResponse.error("参数不能为空");
        }

        // 设置状态为已驳回
        boolean result = commentService.reviewComment(
            comment.getCommentId(),
            Constants.COMMENT_STATUS_REJECT,
            SecurityUtils.getUsername(),
            comment.getReviewRemark());

        return result ? ApiResponse.success() : ApiResponse.error("驳回失败");
    }

    /**
     * 删除评论
     */
    @PreAuthorize("@ss.hasPermi('system:comment:remove')")
    @DeleteMapping("/{commentIds}")
    public ApiResponse<Boolean> remove(@PathVariable Long[] commentIds) {
        if (commentIds == null || commentIds.length == 0) {
            return ApiResponse.error("参数不能为空");
        }

        boolean result = false;
        Long userId = SecurityUtils.getUserId();
        for (Long commentId : commentIds) {
            result = commentService.deleteComment(commentId, userId);
            if (!result) {
                break;
            }
        }

        return result ? ApiResponse.success() : ApiResponse.error("删除失败");
    }

    /**
     * 同步评论到APP
     */
    @PreAuthorize("@ss.hasPermi('system:comment:sync')")
    @PutMapping("/sync/{commentId}")
    public ApiResponse<Boolean> sync(@PathVariable Long commentId) {
        if (commentId == null) {
            return ApiResponse.error("参数不能为空");
        }

        boolean result = commentService.syncCommentToApp(commentId);

        return result ? ApiResponse.success() : ApiResponse.error("同步失败");
    }
}
